//
//  HomePageSingleCase.m
//  UI_A项目
//
//  Created by dlios on 15-1-19.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "HomePageSingleCase.h"
#import <sqlite3.h>
@implementation HomePageSingleCase

+ (instancetype)homePageSingleCase;
{
    static HomePageSingleCase *homePageSC = nil;
    
    if (homePageSC == nil ) {
        homePageSC = [[HomePageSingleCase alloc] init];
    }
    return homePageSC;
}

static sqlite3 *db = nil;

- (void)openDB
{
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *pathFile = [array lastObject];
    
    NSString *sqlitePathFile = [pathFile stringByAppendingPathComponent:@"HomePage.sqlite"];
    NSLog(@"你这么傻逼,你家里人你知道么? = %@", sqlitePathFile);
    
    int result = sqlite3_open(sqlitePathFile.UTF8String, &db);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
        
        NSString *sql = @"CREATE TABLE IF NOT EXISTS homePage(number INTEGER PRIMARY KEY AUTOINCREMENT, longTitle TEXT, pic TEXT, shortTitle TEXT, uid TEXT, title TEXT, name TEXT, cover_path TEXT, id TEXT)";
        int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, nil);
        
        if (result == SQLITE_OK) {
            NSLog(@"创建表成功");
        }else
        {
            NSLog(@"创建表失败");
        }
        
    }else
    {
        NSLog(@"数据库打开失败");
    }
    
}
- (void)creatHomePageSC
{

    NSString *sql = @"CREATE TABLE IF NOT EXISTS homePage(number INTEGER PRIMARY KEY AUTOINCREMENT, longTitle TEXT, pic TEXT, shortTitle TEXT, uid TEXT, title TEXT, name TEXT, cover_path TEXT, id TEXT)";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }


}
- (void)insertHomePageSC:(HomeModel *)homePageSC
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO homePage(longTitle, pic, shortTitle, uid, title, name, cover_path, id)values('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", homePageSC.longTitle, homePageSC.pic, homePageSC.shortTitle, homePageSC.uid, homePageSC.title, homePageSC.name, homePageSC.cover_path, homePageSC.id];
    int result = sqlite3_exec(db, insertSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else
    {
        NSLog(@"插入失败");
    }
    
}
- (void)deleteHomePageSC
{
    NSString *deleteSql = [NSString stringWithFormat:@"DROP TABLE homePage"];
    
    int result = sqlite3_exec(db, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除成功");
    }
    
}
- (NSMutableArray *)selectHomePageSC
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *seleteSql = @"SELECT * FROM homePage";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare(db, seleteSql.UTF8String, -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            // int number = sqlite3_column_int(stmt, 0);
            
            const unsigned char *longTitle = sqlite3_column_text(stmt, 1);
            
            const unsigned char *pic = sqlite3_column_text(stmt, 2);
            
            const unsigned char *shortTitle = sqlite3_column_text(stmt, 3);
            
            const unsigned char *uid = sqlite3_column_text(stmt, 4);
            
            const unsigned char *title = sqlite3_column_text(stmt, 5);
            
            const unsigned char *name = sqlite3_column_text(stmt, 6);
            
            const unsigned char *cover_path = sqlite3_column_text(stmt, 7);
            
            const unsigned char *id = sqlite3_column_text(stmt, 8);
            
            NSString *longTitleStr = [NSString stringWithUTF8String:(const char *)longTitle];
            
            NSString *picStr = [NSString stringWithUTF8String:(const char *)pic];
            
            NSString *shortTitleStr = [NSString stringWithUTF8String:(const char *)shortTitle];
            
            NSString *uidStr = [NSString stringWithUTF8String:(const char *)uid];
            
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            
            NSString *cover_pathStr = [NSString stringWithUTF8String:(const char *)cover_path];
            
            NSString *idStr = [NSString stringWithUTF8String:(const char *)id];
            
            HomeModel *homeModel = [[HomeModel alloc] init];
            
            homeModel.longTitle = longTitleStr;
            
            homeModel.pic = picStr;
            
            homeModel.shortTitle = shortTitleStr;
            
            homeModel.uid = uidStr;
            
            homeModel.title = titleStr;
            
            homeModel.name = nameStr;
            
            homeModel.cover_path = cover_pathStr;
            
            homeModel.id = idStr;
            
            [array addObject:homeModel];
        }
        
    }else
    {
        NSLog(@"查询失败");
    }

    return array;
}



















@end
